package com.info.export;

import com.info.domain.Info;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import util.excel.AbstractExportExcel;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static com.google.common.collect.Maps.newIdentityHashMap;
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

/**
 * Created by sola on 2018/1/12.
 */
public class InfoExportExcel extends AbstractExportExcel {
    private List<Info> infoList = null;

    public InfoExportExcel(List<Info> infoList) {
        this.infoList = infoList;
    }

    @Override
    protected String getExcelUri() {
        return "resource/excel/info.xls";
    }

    @Override
    protected void buildExcel(HSSFWorkbook workBook) {
        HSSFSheet hssfSheet = workBook.createSheet();

        List<Info> infoList = this.infoList;
        Integer rowIndex = 1;
        for (Info info : infoList) {
            setSheetValue(hssfSheet, rowIndex, 0, rowIndex);
            setSheetValue(hssfSheet, rowIndex, 1, "1");
            setSheetValue(hssfSheet, rowIndex, 2, "1");
            setSheetValue(hssfSheet, rowIndex, 3, "1");
            rowIndex++;
        }
    }

    @Override
    protected HSSFWorkbook buildNewExcel() {
        HSSFWorkbook workBook = new HSSFWorkbook();
        HSSFSheet hssfSheet = workBook.createSheet();

        List<Info> infoList = this.infoList;
        Integer rowIndex = 0;
        setSheetValue(hssfSheet, rowIndex, 0, "序号");
        setSheetValue(hssfSheet, rowIndex, 1, "ID");
        setSheetValue(hssfSheet, rowIndex, 2, "姓名");
        setSheetValue(hssfSheet, rowIndex, 3, "版本");
        rowIndex++;
        for (Info info : infoList) {
            setSheetValue(hssfSheet, rowIndex, 0, rowIndex);
            setSheetValue(hssfSheet, rowIndex, 1, info.getId());
            setSheetValue(hssfSheet, rowIndex, 2, info.getUserName());
            setSheetValue(hssfSheet, rowIndex, 3, info.getVersion());
            rowIndex++;
        }

        return workBook;
    }

    public static void main(String[] args) {
        File file = new File("D:/javaClass/www.xls");

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);

            List<Info> infoList = newArrayList();
            infoList.add(new Info());
            infoList.add(new Info());
            //Map<String,List<Info>> map = newHashMapWithExpectedSize(1);
            //map.put("infoList",infoList);
            HSSFWorkbook workBook = new HSSFWorkbook();
            InfoExportExcel infoExportExcel = new InfoExportExcel(infoList);
            infoExportExcel.buildExcel(workBook);

            workBook.write(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
